package com.css.gdzj.gdzjbxgd.modules.reportcontral.repository.impl;
import com.css.gdzj.gdzjbxgd.modules.reportcontral.entity.BxgdReportContralEntity;
import com.css.gdzj.gdzjbxgd.modules.reportcontral.repository.BxgdReportContralNativeRepository;
import com.css.gdzj.gdzjbxgd.sys.user.form.LoginForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @author z
 * @date 2020-12-11
 */
@Repository
public class BxgdReportContralRepositoryImpl implements BxgdReportContralNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(BxgdReportContralRepositoryImpl.class);


    @Override
    public List<BxgdReportContralEntity> queryUserInfos(String loginName, String userName, String userPost, String userRank, String userUnit, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from user_info ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!StringUtils.isEmpty(userName)) {
            whereSql.append(" and LOGIN_NAME like :loginName");
        }
        if(!StringUtils.isEmpty(userName)) {
            whereSql.append(" and USER_NAME like :userName");
        }
        if(!StringUtils.isEmpty(userName)) {
            whereSql.append(" and USER_POST like :userPost");
        }
        if(!StringUtils.isEmpty(userName)) {
            whereSql.append(" and USER_NAME like :userRank");
        }
        if(!StringUtils.isEmpty(userUnit)) {
            whereSql.append(" and USER_RANK like :userUnit");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), BxgdReportContralEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from user_info ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if(!StringUtils.isEmpty(userName)) {
            query.setParameter("loginName", "%" + loginName + "%");
            countQuery.setParameter("loginName", "%" + loginName + "%");
        }
        if(!StringUtils.isEmpty(userName)) {
            query.setParameter("userName", "%" + userName + "%");
            countQuery.setParameter("userName", "%" + userName + "%");
        }
        if(!StringUtils.isEmpty(userName)) {
            query.setParameter("userPost", "%" + userPost + "%");
            countQuery.setParameter("userPost", "%" + userPost + "%");
        }
        if(!StringUtils.isEmpty(userName)) {
            query.setParameter("userRank", "%" + userRank + "%");
            countQuery.setParameter("userRank", "%" + userRank + "%");
        }
        if(!StringUtils.isEmpty(userUnit)) {
            query.setParameter("userUnit", "%" + userUnit + "%");
            countQuery.setParameter("userUnit", "%" + userUnit + "%");
        }

        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<BxgdReportContralEntity> list = query.getResultList();

        return list;
    }

}
